import pkg_resources

from turbogears.widgets import CSSLink, JSLink, Widget, WidgetDescription, \
                               register_static_directory

from gibethemetechgeneral2008a import release
resource_name = "gibethemetechgeneral2008a-%s" % (release.version,)
resource_dir = pkg_resources.resource_filename('gibethemetechgeneral2008a', "static")
register_static_directory(resource_name, resource_dir)

from gibethemetechgeneral2008a.relativetime import timesince, timeuntil
import routes
import cherrypy
from gibe.util import utcnow
from gibe.feeds import FeedLink

class GibeThemeTechGeneral2008a(Widget): 
    resource_name = resource_name
    version = release.version
    css = [
        CSSLink(resource_name, 'css/style.css'),
    ]
    javascript = [
    ]

    def __init__(self, *args, **kw):
        super(GibeThemeTechGeneral2008a, self).__init__(*args, **kw)
        self._nav1 = cherrypy.config.get('gibethemetechgeneral2008a.nav1', [])
        self._nav2 = cherrypy.config.get('gibethemetechgeneral2008a.nav2', [])


    def filter_widgets(self, widgets):
        import gibe.tags.widgets
        print ("-" * 50 + "\n") * 3
        for widget in widgets:
            if getattr(widget, "name", None) in ['css/tags.css']:
                continue
            if getattr(widget, "name", None) in ['css/prettify.css']:
                continue
            if getattr(widget, "name", None) in ['javascript/prettify.js']:
                continue
            if getattr(widget, "src", None) in ['prettyPrint()']:
                continue
            if isinstance(widget, gibe.tags.widgets.TagPostDisplayWidget):
                continue
            if isinstance(widget, gibe.tags.widgets.TagDisplayWidget):
                continue
            print widget
            yield widget
        print ("-" * 50 + "\n") * 3

    def timesince(self, d, now = None):
        if now is None:
            now = utcnow()
        return timesince(d, now)

    def timeuntil(self, d, now = None):
        if now is None:
            now = utcnow()
        return timeuntil(d, now)

    @property
    def nav1(self):
        for k, v in self._nav1:
            yield (k, v)

    @property
    def nav2(self):
        for k, v in self._nav2:
            if v.startswith("tag:"):
                tagname = v[4:]
                v = routes.url_for('tags', tagname=tagname)
            yield (k, v)

GibeThemeTechGeneral2008a = GibeThemeTechGeneral2008a()
